﻿Imports System.Data.OracleClient
Imports System.IO
Imports System.Collections.Generic
Imports System.Web.Security
Imports System.Data

Public Class BSCSConfigReportPage
    Inherits System.Web.UI.Page

    Dim ConfigId As Integer = -1
    Dim OldConfigId As Integer = -2
    Dim PageIndex As Integer = 1
    Dim PageSize As Integer = 25
    Dim OldPageSize As Integer = 25
    Dim MaxPageSize As Integer
    Dim RowCount As Integer
    Dim SearchStatement As String
    Dim ScreenName As String = ""
    Dim RN As Integer = 0
    Dim GridSourceTable As DataTable
    Dim TreeViewSelectedNode As TreeNode
    Dim ReportEngine As New BSCSConfigReport
    Dim ColoumnName As String = ""
    Dim Coloumns(5) As DropDownList
    Dim Operators(5) As DropDownList
    Dim Values(5) As TextBox
    Dim Logics(4) As DropDownList
    Dim QueryDDLValue1 As String
    Dim QueryDDLValue2 As String
    Dim QueryDDLValue3 As String
    Dim QueryDDLValue4 As String
    Dim QueryDDLValue5 As String
    Const DefaultPageIndex As Integer = 1
    Const DefaultOldConfigId As Integer = 0
    Const DefaultQueryFilterNumber As Integer = 5


    Private Sub SetViewStateValues()

        If ViewState("PageIndex") Is Nothing Then
            ViewState("PageIndex") = DefaultPageIndex
        End If

    End Sub

    Private Sub GetViewStateVariables()

        ConfigId = ViewState("ConfigId")
        OldConfigId = ViewState("OldConfigId")
        PageIndex = ViewState("PageIndex")
        PageSize = ViewState("PageSize")
        RowCount = ViewState("RowCount")
        SearchStatement = ViewState("SearchStatement")
        ScreenName = ViewState("ScreenName")
        OldPageSize = ViewState("OldPageSize")
        MaxPageSize = ViewState("MaxPageSize")
        RN = ViewState("RN")
        QueryDDLValue1 = ViewState("QueryClm1")
        QueryDDLValue2 = ViewState("QueryClm2")
        QueryDDLValue3 = ViewState("QueryClm3")
        QueryDDLValue4 = ViewState("QueryClm4")
        QueryDDLValue5 = ViewState("QueryClm5")

    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If ConfigId <> 0 Then
            LoadTree()
            If IsPostBack Then
                GetViewStateVariables()
                ExpandTreeMenu()
            End If
            SetViewStateValues()
        End If

    End Sub

    Protected Sub LoadTree()

        TreeMenu.Nodes.Clear()
        Dim TreeNodeList As TreeView
        TreeNodeList = ReportEngine.GetMenu()

        CopyTreeView(TreeNodeList, TreeMenu)

    End Sub

    Private Function GetSortingExpression(_ConfigId As Integer, _OldConfigId As Integer) As String

        Dim SortingDirection As String

        If _ConfigId <> _OldConfigId Then
            Return ""
        End If
        If SortDirection = SortDirection.Ascending Then
            SortDirection = SortDirection.Descending
            SortingDirection = " DESC"
        Else
            SortDirection = SortDirection.Ascending
            SortingDirection = " ASC"
        End If

        If ViewState("SortExpression") Is Nothing Then
            Return ""
        Else
            Return ViewState("SortExpression") + SortingDirection
        End If

    End Function

    Private Function GetPageIndex() As Integer

        If ViewState("PageIndex") Is Nothing Then
            Return DefaultPageIndex
        Else
            Return ViewState("PageIndex")
        End If

    End Function

    Private Function GetSearchStatement() As String
        Return SearchStatement
    End Function

    Protected Sub SortRecords(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles QueryShownGrid.Sorting

        QueryFilterValuesCapture()

        Dim SortExpression As String = e.SortExpression
        ConfigId = ViewState("ConfigId")
        OldConfigId = ConfigId

        ViewState("SortExpression") = SortExpression

        GridDataBind(ConfigId, OldConfigId, PageIndex, PageSize)

        ExpandTreeMenu()
        ReFillQueryFilterValues()

    End Sub

    Public Property SortDirection() As SortDirection

        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return DirectCast(ViewState("SortDirection"), SortDirection)
        End Get

        Set(ByVal value As SortDirection)
            ViewState("SortDirection") = value
        End Set

    End Property

    Private Sub TreeMenu_SelectedNodeChanged(sender As Object, e As EventArgs) Handles TreeMenu.SelectedNodeChanged

        PageIndex = DefaultPageIndex
        PageSize = CType(PageSizeDDL.SelectedItem.Value, Integer)
        ViewState("PageIndex") = PageIndex
        ViewState("PageSize") = PageSize
        

        If IsNumeric(TreeMenu.SelectedNode.Value) Then

            SetDefaultValuesForControls()
            SearchStatement = ""
            ViewState("SearchStatement") = ""
            OldConfigId = ConfigId
            ConfigId = TreeMenu.SelectedNode.Value
            ViewState("ConfigId") = ConfigId
            RowCount = GetRowCount(ConfigId)
            ViewState("RowCount") = RowCount
            SetMaximumPageSize(PageSize, ConfigId)
            ViewState("MaxPageSize") = MaxPageSize


            GridDataBind(ConfigId, OldConfigId, DefaultPageIndex, PageSize, SearchStatement)

            If TableDescription.Visible = False Then
                TableDescription.Visible = True
            End If
            TableDescription.Text = ReportEngine.GetUsageDescription(ConfigId)


            ScreenName = TreeMenu.SelectedNode.Text.ToString()
            ViewState("ScreenName") = ScreenName

            Dim Page As Double
            Page = RowCount / PageSize
            Page = Math.Ceiling(Page)

            PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()

            ExpandTreeMenu()
            AddButtons()
            CleanButtonClick(sender, Nothing)
            FillPageNumbers()
            PageNumberDDL.Text = DefaultPageIndex

        Else
            ConfigId = ViewState("ConfigId")
            Exit Sub
        End If

    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(control As Control)
        ' this is just an overriding function which does not contain any code line.
    End Sub
    Private Function FileNameConvert(_FileName As String) As String
        _FileName = _FileName.Replace("Ş", "S")
        _FileName = _FileName.Replace("Ü", "U")
        _FileName = _FileName.Replace("Ğ", "G")
        _FileName = _FileName.Replace("İ", "I")
        _FileName = _FileName.Replace("Ö", "O")
        _FileName = _FileName.Replace("Ç", "C")
        _FileName = _FileName.Replace("ş", "s")
        _FileName = _FileName.Replace("ü", "u")
        _FileName = _FileName.Replace("ğ", "g")
        _FileName = _FileName.Replace("ı", "i")
        _FileName = _FileName.Replace("ö", "o")
        _FileName = _FileName.Replace("ç", "c")
        Return _FileName
    End Function
    Public Sub ExportToExcelFile()
        Try
            QueryShownGrid.AllowPaging = False
            GridDataBind(ConfigId, ConfigId, Nothing, Nothing)

            Response.Clear()
            Response.Buffer = True

            Dim DateIndicator As Date = Date.Now
            Dim FileName As String = ScreenName + " " + DateIndicator.ToString() + ".xls"

            Response.Charset = ""
            Response.ContentEncoding = System.Text.Encoding.Unicode
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble())

            FileName = FileNameConvert(FileName)
            Response.AddHeader("content-disposition", _
                 "attachment;filename=" + FileName)
            Response.ContentType = "application/vnd.ms-excel"



            Dim StringWriter As New StringWriter()
            Dim HtmlTextWriter As New HtmlTextWriter(StringWriter)


            For i As Integer = 0 To QueryShownGrid.Rows.Count - 1
                QueryShownGrid.Rows(i).Attributes.Add("class", "textmode")
            Next

            QueryShownGrid.RenderControl(HtmlTextWriter)

            Dim style As String = "<style> .textmode{mso-number-format:\@;}</style>"
            Response.Write(style)
            Response.Output.Write(StringWriter.ToString())
            Response.Flush()

            GridDataBind(ConfigId, OldConfigId, PageIndex, PageSize, SearchStatement)

        Catch ex As Exception

            Dim LogMessage As String
            LogMessage = "Excel dosyasına aktarma işlemi gerçekleşmedi " & ex.ToString()

            Dim LengthofExMessage As Integer
            LengthofExMessage = ex.ToString.Length - 1
            If LengthofExMessage >= 3900 Then
                LengthofExMessage = 3900
            End If

            ReportEngine.LogFailure(LogMessage.Substring(0, LengthofExMessage))
            Dim FailureMessage As Object = MsgBox("Excel dosyasına aktarma işlemi gerçekleşmedi", MsgBoxStyle.Information)
        Finally
            Response.End()
        End Try

    End Sub

    Private Sub GridDataBind(GridSourceTable As DataTable)

        GridSourceTable = ReportEngine.GetConfigSQLDataTable(ViewState("ConfigId"), GetSortingExpression(TreeMenu.SelectedNode.Value, ConfigId), GetPageIndex(), Convert.ToInt32(PageSizeDDL.SelectedItem.Value), GetSearchStatement())

        QueryShownGrid.DataSource = GridSourceTable
        QueryShownGrid.DataBind()
        FillQueryFilterValues(GridSourceTable)

    End Sub

    Private Sub GridDataBind(_ConfigId As Integer, _OldConfigId As Integer, _PageIndex As Integer, _PageSize As Integer)
        Dim DataGridTable As DataTable
        DataGridTable = ReportEngine.GetConfigSQLDataTable(_ConfigId, GetSortingExpression(_ConfigId, _OldConfigId), _PageIndex, _PageSize, SearchStatement)
        If IsNothing(DataGridTable) = False Then
            GridSourceTable = ReportEngine.GetConfigSQLDataTable(_ConfigId, GetSortingExpression(_ConfigId, _OldConfigId), _PageIndex, _PageSize, SearchStatement)
            If (GridSourceTable.Columns(GridSourceTable.Columns.Count - 1)).Caption = "RN" Then
                RN = GridSourceTable.Columns.Count - 1
                ViewState("RN") = RN
            Else
                RN = 0
                ViewState("RN") = RN
            End If
            QueryShownGrid.DataSource = GridSourceTable

            QueryShownGrid.DataBind()
            FillQueryFilterValues(GridSourceTable)
        Else
            QueryShownGrid.DataSource = Nothing
            QueryShownGrid.DataBind()

        End If

        If IsNothing(QueryShownGrid.DataSource) = True Then
            dialog.Visible = True
        Else
            dialog.Visible = False
        End If
    End Sub

    Private Sub GridDataBind(_ConfigId As Integer, _OldConfigId As Integer, _PageIndex As Integer, _PageSize As Integer, _SearchStatement As String)

        GridSourceTable = ReportEngine.GetConfigSQLDataTable(_ConfigId, GetSortingExpression(_ConfigId, _OldConfigId), _PageIndex, _PageSize, _SearchStatement)
        If (GridSourceTable.Columns(GridSourceTable.Columns.Count - 1)).Caption = "RN" Then
            RN = GridSourceTable.Columns.Count - 1
            ViewState("RN") = RN
        Else
            RN = 0
            ViewState("RN") = RN
        End If

        QueryShownGrid.DataSource = GridSourceTable
        QueryShownGrid.DataBind()

        FillQueryFilterValues(GridSourceTable)

    End Sub

    Protected Sub CopyTreeView(SourceTreeNodeList As TreeView, TargetTreeNodeList As TreeView)

        For MainNodes As Integer = 0 To SourceTreeNodeList.Nodes.Count - 1
            TargetTreeNodeList.Nodes.Add(SourceTreeNodeList.Nodes(MainNodes))
        Next

    End Sub

    Private Sub ExpandTreeMenu()

        For Each node As TreeNode In TreeMenu.Nodes
            node.CollapseAll()
        Next

        For SubNode As Integer = 0 To TreeMenu.Nodes(0).ChildNodes.Count - 1
            For GrandChildNode As Integer = 0 To TreeMenu.Nodes(0).ChildNodes(SubNode).ChildNodes.Count - 1
                If (TreeMenu.Nodes(0).ChildNodes(SubNode).ChildNodes(GrandChildNode).Value = ViewState("ConfigId")) Then
                    TreeMenu.Nodes(0).ChildNodes(SubNode).ChildNodes(GrandChildNode).Expand()
                    TreeMenu.Nodes(0).ChildNodes(SubNode).ChildNodes(GrandChildNode).Parent.Expand()
                    TreeMenu.Nodes(0).Expand()
                End If
            Next
        Next

    End Sub
    Private Sub ClearQueryFilterValues()

        QueryFilterValue1.Items.Clear()
        QueryFilterValue1.Items.Add("")
        QueryFilterValue2.Items.Clear()
        QueryFilterValue2.Items.Add("")
        QueryFilterValue3.Items.Clear()
        QueryFilterValue3.Items.Add("")
        QueryFilterValue4.Items.Clear()
        QueryFilterValue4.Items.Add("")
        QueryFilterValue5.Items.Clear()
        QueryFilterValue5.Items.Add("")

    End Sub
    Protected Sub FillQueryFilterValues(GridSourceTable As DataTable)

        ClearQueryFilterValues()

        For row As Integer = 0 To GridSourceTable.Columns.Count - 2
            QueryFilterValue1.Items.Add(GridSourceTable.Columns(row).ToString())
            QueryFilterValue2.Items.Add(GridSourceTable.Columns(row).ToString())
            QueryFilterValue3.Items.Add(GridSourceTable.Columns(row).ToString())
            QueryFilterValue4.Items.Add(GridSourceTable.Columns(row).ToString())
            QueryFilterValue5.Items.Add(GridSourceTable.Columns(row).ToString())
        Next

    End Sub

    Protected Function GetRowCount(_ConfigId As Integer) As Integer

        Return ReportEngine.GetRowCount(_ConfigId, GetSearchStatement())

    End Function

    Protected Sub AddQueryFilterValuesToArray()

        Coloumns(0) = QueryFilterValue1
        Coloumns(1) = QueryFilterValue2
        Coloumns(2) = QueryFilterValue3
        Coloumns(3) = QueryFilterValue4
        Coloumns(4) = QueryFilterValue5

        Operators(0) = OperatorValue1
        Operators(1) = OperatorValue2
        Operators(2) = OperatorValue3
        Operators(3) = OperatorValue4
        Operators(4) = OperatorValue5

        Values(0) = QueryValues1
        Values(1) = QueryValues2
        Values(2) = QueryValues3
        Values(3) = QueryValues4
        Values(4) = QueryValues5

        Logics(0) = LogicValue1
        Logics(1) = LogicValue2
        Logics(2) = LogicValue3
        Logics(3) = LogicValue4

    End Sub

    Protected Sub ChangeEvent(sender As Object)

        GridSourceTable = ReportEngine.GetConfigSQLDataTable(ConfigId, "", DefaultPageIndex, CType(PageSizeDDL.SelectedItem.Value, Integer), GetSearchStatement())

        Dim ListOfOperators As New List(Of String)()
        ListOfOperators.Add("")
        If IsNothing(GridSourceTable) = False Then
            For Coloumns As Integer = 0 To GridSourceTable.Columns.Count - 1
                If sender.SelectedItem.Text = GridSourceTable.Columns(Coloumns).ToString() Then
                    Dim OperatorObject As DropDownList

                    With sender
                        If .Equals(QueryFilterValue1) Then
                            OperatorObject = OperatorValue1
                        ElseIf .Equals(QueryFilterValue2) Then
                            OperatorObject = OperatorValue2
                        ElseIf .Equals(QueryFilterValue3) Then
                            OperatorObject = OperatorValue3
                        ElseIf .Equals(QueryFilterValue4) Then
                            OperatorObject = OperatorValue4
                        ElseIf .Equals(QueryFilterValue5) Then
                            OperatorObject = OperatorValue5
                        End If
                    End With

                    If GridSourceTable.Columns(Coloumns).DataType.ToString() = "System.String" Then

                        OperatorObject.Items.Clear()
                        ListOfOperators.Add("like")
                        ListOfOperators.Add("=")
                        OperatorObject.DataSource = ListOfOperators
                        OperatorObject.DataBind()
                    Else
                        OperatorObject.Items.Clear()
                        ListOfOperators.Add("=")
                        ListOfOperators.Add("in")
                        ListOfOperators.Add("<")
                        ListOfOperators.Add("<=")
                        ListOfOperators.Add(">")
                        ListOfOperators.Add(">=")
                        OperatorObject.DataSource = ListOfOperators
                        OperatorObject.DataBind()
                    End If
                End If
            Next
        End If
    End Sub

    Protected Sub AddButtons()

        AddButton1.Visible = True
        AddButton2.Visible = False
        AddButton3.Visible = False
        AddButton4.Visible = False

    End Sub

    Protected Sub InitializeQueryVariables()

        Dim Form As Control = Me.FindControl("Form1")
        Dim ControlTextBox As Control
        Dim ControlDropDownList As Control

        For Each ControlTextBox In Form.Controls
            Dim TextBoxObject As TextBox
            If TypeOf ControlTextBox Is TextBox Then
                TextBoxObject = CType(ControlTextBox, TextBox)
                TextBoxObject.Visible = False
            End If
        Next

        For Each ControlDropDownList In Form.Controls
            Dim DropDownListObject As DropDownList
            If TypeOf ControlDropDownList Is DropDownList Then
                DropDownListObject = CType(ControlDropDownList, DropDownList)
                If DropDownListObject.ToolTip = "" Then
                    'ddl.Text = String.Empty
                    DropDownListObject.Visible = False
                End If
            End If
        Next

        QueryFilterValue1.Visible = True
        OperatorValue1.Visible = True
        QueryValues1.Visible = True
        PageNumberDDL.Visible = True

    End Sub

    Protected Sub CleanQueryVariables()

        Dim Form As Control = Me.FindControl("Form1")
        Dim ControlTextBox As Control
        Dim ControlDropDownList As Control

        For Each ControlTextBox In Form.Controls
            Dim TextBoxObject As TextBox
            If TypeOf ControlTextBox Is TextBox Then
                TextBoxObject = CType(ControlTextBox, TextBox)
                TextBoxObject.Text = String.Empty
            End If
        Next

        For Each ControlDropDownList In Form.Controls
            Dim DropDownListObject As DropDownList
            If TypeOf ControlDropDownList Is DropDownList Then
                DropDownListObject = CType(ControlDropDownList, DropDownList)
                If DropDownListObject.ToolTip = "" Then
                    DropDownListObject.Text = String.Empty

                End If
            End If
        Next

    End Sub

    Protected Sub PageSizeDDL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PageSizeDDL.SelectedIndexChanged

        QueryFilterValuesCapture()
        OldPageSize = PageSize
        PageSize = CType(PageSizeDDL.SelectedItem.Value, Integer)

        Dim PageSizer As Double
        PageSizer = PageIndex * (OldPageSize / PageSize)

        PageIndex = Math.Ceiling(PageSizer)
        SetMaximumPageSize(PageSize, ConfigId)
        ViewState("MaxPageSize") = MaxPageSize

        PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()
        ViewState("PageIndex") = PageIndex
        ViewState("PageSize") = PageSize
        ViewState("OldPageSize") = OldPageSize

        GridDataBind(ConfigId, DefaultOldConfigId, PageIndex, PageSize)
        AddButtons()
        ExpandTreeMenu()
        FillPageNumbers()

        ReFillQueryFilterValues()

    End Sub

    Public Sub SetMaximumPageSize(_PageSize As Integer, _ConfigID As Integer)

        Dim Page As Double
        Page = RowCount / _PageSize
        MaxPageSize = Math.Ceiling(Page)
        ViewState("MaxPageSize") = MaxPageSize

    End Sub

    Protected Sub HideRNColoumn()

        For i As Integer = 0 To QueryShownGrid.Columns.Count - 1
            If i = QueryShownGrid.Columns.Count - 1 Then
                QueryShownGrid.Columns(i).Visible = False
            End If
        Next

    End Sub

    Protected Sub QueryShownGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles QueryShownGrid.RowDataBound

        If RN > 0 Then
            e.Row.Cells(RN).Visible = False
        End If

    End Sub

    Protected Sub NextPageButtonClick(sender As Object, e As ImageClickEventArgs) Handles NextPageButton.Click

        QueryFilterValuesCapture()
        PageIndex = PageIndex + 1

        If PageIndex > MaxPageSize Then
            PageIndex = PageIndex - 1
        End If

        ViewState("PageIndex") = PageIndex
        PageSize = CType(PageSizeDDL.SelectedItem.Value, Integer)
        ViewState("PageSize") = PageSize

        PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()

        GridDataBind(ConfigId, DefaultOldConfigId, PageIndex, PageSize)
        AddButtons()
        ExpandTreeMenu()
        FillPageNumbers()
        PageNumberDDL.Text = PageIndex.ToString()

        ReFillQueryFilterValues()

    End Sub

    Protected Sub PreviousPageButtonClick(sender As Object, e As ImageClickEventArgs) Handles PreviousPageButton.Click

        QueryFilterValuesCapture()
        PageIndex = PageIndex - 1
        If PageIndex = 0 Then
            PageIndex = DefaultPageIndex
        End If

        Dim Page As Double
        Page = RowCount / PageSize
        Page = Math.Ceiling(Page)

        ViewState("PageIndex") = PageIndex
        PageSize = CType(PageSizeDDL.SelectedItem.Value, Integer)
        ViewState("PageSize") = PageSize

        PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()

        GridDataBind(ConfigId, DefaultOldConfigId, PageIndex, PageSize)
        AddButtons()
        ExpandTreeMenu()
        FillPageNumbers()
        PageNumberDDL.Text = PageIndex.ToString()

        ReFillQueryFilterValues()

    End Sub

    Private Sub FirstorLastPageEvent(_PageIndex As Integer)

        QueryFilterValuesCapture()
        PageIndex = _PageIndex
        ViewState("PageIndex") = PageIndex
        PageSize = CType(PageSizeDDL.SelectedItem.Value, Integer)
        ViewState("PageSize") = PageSize

        PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()

        GridDataBind(ConfigId, DefaultOldConfigId, PageIndex, PageSize)
        AddButtons()
        ExpandTreeMenu()
        FillPageNumbers()
        PageNumberDDL.Text = PageIndex.ToString()

        ReFillQueryFilterValues()

    End Sub

    Protected Sub FirstPageButtonClick(sender As Object, e As ImageClickEventArgs) Handles FirstPageButton.Click

        FirstorLastPageEvent(DefaultPageIndex)

    End Sub

    Protected Sub LastPageButtonClick(sender As Object, e As ImageClickEventArgs) Handles LastPageButton.Click

        FirstorLastPageEvent(MaxPageSize)

    End Sub

    Public Sub SetDefaultValuesForControls()

        AddButton1.Visible = True
        QueryShownGrid.Visible = True
        QueryFilterValue1.Visible = True
        OperatorValue1.Visible = True
        QueryValues1.Visible = True
        Panel1.Visible = True
        Panel2.Visible = True
        FiltreKolon.Visible = True
        Operatorler.Visible = True
        Degerler.Visible = True
        PageNumber.Visible = True
        PageNumberTitle.Visible = True
        QueryGridDiv.Visible = True
        CommandLine.Visible = True
        TableUp.Visible = True

    End Sub

    Public Sub FillPageNumbers()

        PageNumberDDL.Items.Clear()

        For PageNumbers As Integer = DefaultPageIndex To MaxPageSize
            PageNumberDDL.Items.Add(PageNumbers.ToString())
        Next
        If PageIndex <= MaxPageSize Then
            If PageIndex = 0 Then
                PageIndex = DefaultPageIndex
            End If
            PageNumberDDL.Text = PageIndex
        Else
            PageNumberDDL.Text = MaxPageSize
            PageIndex = MaxPageSize
            ViewState("PageIndex") = PageIndex
        End If

    End Sub

    Protected Sub PageNumberDDL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PageNumberDDL.SelectedIndexChanged

        QueryFilterValuesCapture()
        PageIndex = PageNumberDDL.SelectedItem.Value

        Dim Page As Double
        Page = RowCount / PageSize
        Page = Math.Ceiling(Page)

        ViewState("PageIndex") = PageIndex
        PageSize = CType(PageSizeDDL.SelectedItem.Value, Integer)
        ViewState("PageSize") = PageSize

        PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()

        GridDataBind(ConfigId, OldConfigId, PageIndex, PageSize)
        AddButtons()
        ExpandTreeMenu()
        ReFillQueryFilterValues()

    End Sub

    Protected Sub QueryFilterValuesCapture()

        QueryDDLValue1 = QueryFilterValue1.Text
        ViewState("QueryDDLValue1") = QueryDDLValue1
        QueryDDLValue2 = QueryFilterValue2.Text
        ViewState("QueryDDLValue2") = QueryDDLValue2
        QueryDDLValue3 = QueryFilterValue3.Text
        ViewState("QueryDDLValue3") = QueryDDLValue3
        QueryDDLValue4 = QueryFilterValue4.Text
        ViewState("QueryDDLValue4") = QueryDDLValue4
        QueryDDLValue5 = QueryFilterValue5.Text
        ViewState("QueryDDLValue5") = QueryDDLValue5

    End Sub

    Protected Sub ReFillQueryFilterValues()

        QueryFilterValue1.Text = QueryDDLValue1
        QueryFilterValue2.Text = QueryDDLValue2
        QueryFilterValue3.Text = QueryDDLValue3
        QueryFilterValue4.Text = QueryDDLValue4
        QueryFilterValue5.Text = QueryDDLValue5

    End Sub

    Protected Sub AddButton1Click(sender As Object, e As ImageClickEventArgs) Handles AddButton1.Click

            If QueryFilterValue1.Text <> "" Then
                AddButton1.Visible = False
                AddButton2.Visible = True
                LogicValue1.Visible = True
                QueryFilterValue2.Visible = True
                OperatorValue2.Visible = True
                QueryValues2.Visible = True
            End If

    End Sub

    Protected Sub AddButton2Click(sender As Object, e As ImageClickEventArgs) Handles AddButton2.Click

        If QueryFilterValue2.Text <> "" Then
            AddButton3.Visible = True
            AddButton2.Visible = False
            LogicValue2.Visible = True
            QueryFilterValue3.Visible = True
            OperatorValue3.Visible = True
            QueryValues3.Visible = True
        End If

    End Sub

    Protected Sub AddButton3Click(sender As Object, e As ImageClickEventArgs) Handles AddButton3.Click

        If QueryFilterValue3.Text <> "" Then
            AddButton3.Visible = False
            AddButton4.Visible = True
            LogicValue3.Visible = True
            QueryFilterValue4.Visible = True
            OperatorValue4.Visible = True
            QueryValues4.Visible = True
        End If

    End Sub

    Protected Sub AddButton4Click(sender As Object, e As ImageClickEventArgs) Handles AddButton4.Click

        If QueryFilterValue4.Text <> "" Then
            AddButton4.Visible = False
            LogicValue4.Visible = True
            QueryFilterValue5.Visible = True
            OperatorValue5.Visible = True
            QueryValues5.Visible = True
        End If

    End Sub

    Protected Sub RunQueryButtonClick(sender As Object, e As ImageClickEventArgs) Handles RunQueryButton.Click
SearchStatement = ""
        ConfigId = ViewState("ConfigId")
        QueryFilterValuesCapture()
        AddQueryFilterValuesToArray()

        Dim IsString As Boolean = False

        Dim DataColoumnsTable As DataTable
        DataColoumnsTable = ReportEngine.GetConfigSqlResultMetaData(ConfigId)

        For i As Integer = 0 To DefaultQueryFilterNumber - 1
            If Coloumns(i).Text <> "" Then
                For Rows As Integer = 0 To DataColoumnsTable.Rows.Count - 1

                    If Coloumns(i).Text = DataColoumnsTable.Rows(Rows)(0).ToString() And DataColoumnsTable.Rows(Rows)(1).ToString() = "STRING" And Operators(i).Text = "like" Then
                        SearchStatement = SearchStatement + " " + "Upper(" + Coloumns(i).Text + ") " + Operators(i).Text + " "
                        SearchStatement = SearchStatement + " upper('%" + Values(i).Text + "%')" + " "
                        IsString = True
                    End If
                    If Coloumns(i).Text = DataColoumnsTable.Rows(Rows)(0).ToString() And DataColoumnsTable.Rows(Rows)(1).ToString() = "STRING" And Operators(i).Text = "=" Then
                        SearchStatement = SearchStatement + " " + "Upper(" + Coloumns(i).Text + ") " + Operators(i).Text + " "
                        SearchStatement = SearchStatement + " upper('" + Values(i).Text + "') "
                        IsString = True
                    End If
                Next
                If IsString = False And Operators(i).Text = "in" Then
                    SearchStatement = SearchStatement + Coloumns(i).Text + " " + Operators(i).Text + " ( "
                    SearchStatement = SearchStatement + " " + Values(i).Text + " )"
                End If
                If IsString = False And Operators(i).Text <> "in" Then
                    SearchStatement = SearchStatement + Coloumns(i).Text + " " + Operators(i).Text + " "
                    SearchStatement = SearchStatement + " " + Values(i).Text + " "
                End If

                SearchStatement = SearchStatement + " " + Logics(i).SelectedItem.Value + " "
                IsString = False
            End If
        Next

        SearchStatement = SearchStatement.Replace(vbLf, " ")
        ViewState("SearchStatement") = SearchStatement

        GridDataBind(ConfigId, OldConfigId, 1, PageSize)
        ExpandTreeMenu()
        ReFillQueryFilterValues()

        RefreshPageNumbersForSearchStatement()
        FillPageNumbers()
        PageNumber.Text = PageIndex.ToString() + " / " + MaxPageSize.ToString()

    End Sub

    Private Sub RefreshPageNumbersForSearchStatement()

        RowCount = GetRowCount(ConfigId)
        ViewState("RowCount") = RowCount
        SetMaximumPageSize(PageSize, ConfigId)
        ViewState("MaxPageSize") = MaxPageSize

        PageNumber.Text = "1" + " / " + MaxPageSize.ToString()
        PageIndex = DefaultPageIndex
        ViewState("PageIndex") = PageIndex
        ViewState("PageSize") = PageSize
        ViewState("OldPageSize") = OldPageSize

        PageNumberDDL.Text = PageIndex.ToString()

    End Sub

    Protected Sub CleanButtonClick(sender As Object, e As ImageClickEventArgs) Handles CleanButton.Click

        CleanQueryVariables()
        ExpandTreeMenu()
        AddButtons()
        InitializeQueryVariables()

        'ConfigId = ViewState("ConfigId")
        PageIndex = DefaultPageIndex
        ViewState("PageIndex") = PageIndex
        SearchStatement = ""
        ViewState("SearchStatement") = SearchStatement
        GridDataBind(ConfigId, DefaultOldConfigId, PageIndex, PageSize, SearchStatement)
        RefreshPageNumbersForSearchStatement()
        FillPageNumbers()
        dialog.Visible = False

    End Sub

    Private Function CheckAuthorization() As Boolean

        Return ReportEngine.CheckAuthorization()

    End Function

    Protected Sub QueryFilterValue1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles QueryFilterValue1.SelectedIndexChanged
        ChangeEvent(sender)
        ExpandTreeMenu()
    End Sub
    Protected Sub QueryFilterValue2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles QueryFilterValue2.SelectedIndexChanged
        ChangeEvent(sender)
        ExpandTreeMenu()
    End Sub
    Protected Sub QueryFilterValue3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles QueryFilterValue3.SelectedIndexChanged
        ChangeEvent(sender)
        ExpandTreeMenu()
    End Sub
    Protected Sub QueryFilterValue4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles QueryFilterValue4.SelectedIndexChanged
        ChangeEvent(sender)
        ExpandTreeMenu()
    End Sub
    Protected Sub QueryFilterValue5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles QueryFilterValue5.SelectedIndexChanged
        ChangeEvent(sender)
        ExpandTreeMenu()
    End Sub

    Protected Sub ExportExcelButton_Click(sender As Object, e As ImageClickEventArgs) Handles ExportExcelButton.Click
        ExportToExcelFile()
    End Sub

    Protected Sub HelpButton_Click(sender As Object, e As ImageClickEventArgs) Handles HelpButton.Click
        Response.Redirect("~/web/page/BSCSConfigReportHelpPage.aspx")
        'Server.Transfer("BSCSConfigReportHelpPage.aspx", True)

    End Sub
End Class